package org.erp.procurement.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import org.erp.common.basic.BaseEntity;
import org.erp.procurement.constant.OrderConstant;
import org.erp.procurement.entity.bo.MiniOrderAddBo;
import org.erp.procurement.enums.OrderStatusEnum;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 线上小程序订单表
 *
 * @author libin
 * @version 1.0
 * @description: 线上小程序订单表
 * @date 2025/11/5 10:34
 **/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@TableName(value = "mini_order", autoResultMap = true)
public class MiniOrder extends BaseEntity {
    /**
     * 自增id
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 订单编号
     */
    private String orderNo;
    /**
     * 采购数量
     */
    private Integer purchaseQuantity;
    /**
     * 验机数量
     */
    private Integer checkQuantity;
    /**
     * 回收数量
     */
    private Integer recycleQuantity;
    /**
     * 回收金额
     */
    private Integer recycleAmount;
    /**
     * 退回数量
     */
    private Integer returnQuantity;
    /**
     * 售卖人id
     */
    private Long sellId;
    /**
     * 售卖人名称
     */
    private String sellName;
    /**
     * 售卖人手机号
     */
    private String sellPhone;
    /**
     * 售卖人地址
     */
    private String sellAddress;
    /**
     * 租户id
     */
    private Long tenantId;
    /**
     * 租户名称
     */
    private String tenantName;
    /**
     * 仓库id
     */
    private Long storeId;
    /**
     * 仓库名称
     */
    private String storeName;
    /**
     * 快递单号
     */
    private String trackingNumber;
    /**
     * 订单状态
     */
    private Integer status;

    public static MiniOrder addBuild(MiniOrderAddBo miniOrderAddBo) {
        return MiniOrder.builder()
                .orderNo(OrderConstant.MINI_PREFIX + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                        + String.valueOf(Math.random()).substring(2, 6))
                .purchaseQuantity(null == miniOrderAddBo.getPurchaseQuantity() ? 1 : miniOrderAddBo.getPurchaseQuantity())
                .checkQuantity(0)
                .recycleQuantity(0)
                .recycleAmount(0)
                .sellId(miniOrderAddBo.getSellId())
                .sellName(miniOrderAddBo.getSellName())
                .sellPhone(miniOrderAddBo.getSellPhone())
                .sellAddress(miniOrderAddBo.getSellAddress())
                .storeId(miniOrderAddBo.getStoreId())
                .storeName(miniOrderAddBo.getStoreName())
                .trackingNumber(StringUtils.isNotBlank(miniOrderAddBo.getTrackingNumber()) ? miniOrderAddBo.getTrackingNumber() : "")
                .status(OrderStatusEnum.TO_BE_SHIPPED.getCode())
                .build();
    }
}
